package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;

import com.bumptech.glide.Glide;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.User;
import com.hengxinguotong.zhihuichengjian.treeview.RecyclerAdapter;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.hengxinguotong.zhihuichengjian.widget.view.CircleImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by liux on 2016/7/11.
 */
public class AddContactsAndMessageAdapter extends RecyclerAdapter<User, AddContactsAndMessageAdapter.ContactsViewHolder> {
    private OnSendMessageListener onSendMessageListener;

    public AddContactsAndMessageAdapter(Context context, List<User> dataList) {
        super(context, dataList);
    }

    public void setOnSendMessageListener(OnSendMessageListener onSendMessageListener) {
        this.onSendMessageListener = onSendMessageListener;
    }

    @Override
    public ContactsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item_add_noticer, null);
        return new ContactsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ContactsViewHolder holder, int position) {
        holder.bindData(position);
    }

    public class ContactsViewHolder extends RecyclerAdapter.BaseViewHolder<User> {
        @BindView(R.id.item_avatar)
        CircleImageView itemAvatar;
        @BindView(R.id.name_tv)
        HXTextView nameTv;
        @BindView(R.id.team_tv)
        HXTextView teamTv;
        @BindView(R.id.company_tv)
        HXTextView companyTv;
        @BindView(R.id.label_tv)
        HXTextView labelTv;
        @BindView(R.id.mobile_tv)
        HXTextView mobileTv;
        @BindView(R.id.checkbox)
        ImageView checkbox;
        @BindView(R.id.send_message_switch)
        Switch sendMessageSwitch;

        public ContactsViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void bindData(final int position) {
            final User user = dataList.get(position);
            String headPortrait = user.getHeadPortrait();
            if(!Utils.isEmpty(headPortrait)){
                headPortrait = headPortrait.substring(0, headPortrait.lastIndexOf(".")) + "_100x100" + headPortrait.substring(headPortrait.lastIndexOf("."));
            }
            Glide.with(context).
                    load(headPortrait)
                    .placeholder(R.mipmap.icon_avater_defalut)
                    .dontAnimate()
                    .error(R.mipmap.icon_avater_defalut)
                    .into(itemAvatar);
            if (user.isChecked()) {
                checkbox.setImageResource(R.mipmap.circle_choice_ligt);
            } else {
                checkbox.setImageResource(R.mipmap.circle_choice);
            }
            if(user.getStatus() == 0){
                sendMessageSwitch.setChecked(false);
            }else {
                sendMessageSwitch.setChecked(true);
            }
            nameTv.setText(user.getName());
            mobileTv.setText(user.getMobile());
            labelTv.setVisibility(View.GONE);
            if (user.getTeamName() != null) {
                teamTv.setVisibility(View.VISIBLE);
                teamTv.setText(user.getTeamName());
            }else {
                teamTv.setVisibility(View.GONE);
            }
            if (user.getProjectCompanyName() != null) {
                companyTv.setVisibility(View.VISIBLE);
                companyTv.setText(user.getProjectCompanyName());
            }else {
                companyTv.setVisibility(View.GONE);
            }
            sendMessageSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    onSendMessageListener.onSendMessage(position, isChecked);
                }
            });
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickListener.onClick(position);
                }
            });
        }
    }

    public interface OnSendMessageListener{
        void onSendMessage(int position, boolean isChecked);
    }
}

